/*
 * Created on May 23, 2006
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package jonaArgumentationSchemes;

import java.util.*;
import java.io.*;
import javax.swing.JList;
import javax.swing.DefaultListModel;
import java.awt.event.*;


/**
 * @author Tijs Zwinkels
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Knowledge implements MouseListener{
	protected static Hashtable knowledge = new Hashtable(13);
	static DefaultListModel knowledgeModel = new DefaultListModel();
	public static JList knowledgeList = new JList(knowledgeModel);
	static DefaultListModel sugestionModel = new DefaultListModel();
	public static JList suggestionList = new JList(sugestionModel);
	
	
	public static void addKnowledge(String naam,String text)
	{
		System.out.println("Knowledge toegevoegd");
		knowledge.put(naam,text);
	}
	
	public static Enumeration getKnowledge()
	{
		return knowledge.keys();
	}
	
	public static void removeKnowledge(String i)
	{
		knowledge.remove(i);
	}
	
	public static String getKnowledge(String i)
	{
		String buffer= (String) knowledge.get(i);
		buffer=buffer.replace("{".charAt(0)," ".charAt(0) );
		buffer=buffer.replace("}".charAt(0)," ".charAt(0) );
		
		return buffer;
	}
	
	public static String getPlainKnowledge(String i)
	{
		return (String) knowledge.get(i);

	}

	
	public static void load()
	{

		try
		{
			FileInputStream fis = new FileInputStream("knowledge.dat");
		    ObjectInputStream ois = new ObjectInputStream(fis);
		    knowledge = (Hashtable) ois.readObject();
		    ois.close();
		    fis.close();
		    
		}
		catch (Exception exception)
		{
		}
	}
	
	public static void save()
	{
		try
		{
			FileOutputStream fos = new FileOutputStream("knowledge.dat");
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(knowledge);
			oos.flush();
			oos.close();
			fos.close();
		}
		catch (Exception exception)
		{
		}
	}

	// Returns Enumeration of keys from knowledge-entries which match the line.
	public static Enumeration suggest(String line)
	{
		sugestionModel.clear(); // clear the list
		
		Vector suggestions = new Vector();
		 for (Enumeration e = knowledge.keys() ; e.hasMoreElements() ;)
		 {
			 int index=0;
			 Object key = e.nextElement();
			 String wholestring = (String) knowledge.get(key);
			 int end,start;
			 //start=wholestring.indexOf("{",index);
			 while (((start=wholestring.indexOf("{",index)) != -1))
			 {
				 end = wholestring.indexOf("}",start);
				 if (end != -1)
				 {
					 System.out.println("Knowledge::suggest - Searching for "+wholestring.substring(start+1,end));
					 if (line.toLowerCase().contains(wholestring.substring(start+1,end).toLowerCase()))
					 {
						 suggestions.add(key);
						 sugestionModel.addElement(key);
					 }
					 index=end;
				 }
				 else
					 index=wholestring.length();
			 }
		 }
		
		/*System.out.println("Suggesting: ");
		for (Enumeration e = suggestions.elements() ; e.hasMoreElements() ;)
			System.out.println((String) e.nextElement());*/	
		 
		return suggestions.elements();
	}
	
	public static void updateKnowledgeList()
	{
		 knowledgeModel.clear();
		 for (Enumeration e = Knowledge.getKnowledge() ; e.hasMoreElements() ;)
			{
				Object name = e.nextElement();
				knowledgeModel.addElement((String) name);
			}		
	}
	
//	The mouselistener.
	public void mouseClicked(MouseEvent e) {
	        if (e.getClickCount() == 2) {
	            int index = suggestionList.locationToIndex(e.getPoint());
	            System.out.println("Double clicked on Item " + index);
	         }
	    }
	
	public void mousePressed(MouseEvent e) {};
	
	public void mouseReleased(MouseEvent e) {};
	
	public void mouseEntered(MouseEvent e) {};
	
	public void mouseExited(MouseEvent e) {};
	
	
}


